package org.example.demo;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.ValidationInfo;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

public class DataSourceDialog extends DialogWrapper {

    private JPanel mainPanel;
    private JComboBox<String> dbTypeComboBox;
    private JTextField hostField;
    private JTextField portField;
    private JTextField databaseField;
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JTextField aiKeyField;

    // 修改构造函数
    public DataSourceDialog(@Nullable Project project, @Nullable DataSourceConfig config) {
        super(project);
        setTitle("配置数据库连接 - 人大金仓");

        init();
        setSize(400, 300);

        // 初始化下拉框
        dbTypeComboBox.addItem("Kingbase");
        dbTypeComboBox.addItem("PostgreSQL");
        dbTypeComboBox.addItem("MySQL");

        if (config != null) {
            dbTypeComboBox.setSelectedItem(config.dbType);
            hostField.setText(config.host);
            portField.setText(String.valueOf(config.port));
            databaseField.setText(config.database);
            usernameField.setText(config.username);
            passwordField.setText(config.password);
            aiKeyField.setText(config.aiKey);
        } else {
            dbTypeComboBox.setSelectedItem("Kingbase");
            hostField.setText("172.17.228.93");
            portField.setText("5426");
            databaseField.setText("test");
            usernameField.setText("root");
            passwordField.setText("12345@Db");
            aiKeyField.setText("你的通义私钥Key");
        }
    }
    @Override
    protected @Nullable JComponent createCenterPanel() {
        return mainPanel;
    }

    @Override
    protected @Nullable ValidationInfo doValidate() {
        if (hostField.getText().trim().isEmpty()) {
            return new ValidationInfo("请输入主机地址", hostField);
        }
        if (usernameField.getText().trim().isEmpty()) {
            return new ValidationInfo("请输入用户名", usernameField);
        }
        try {
            int port = Integer.parseInt(portField.getText().trim());
            if (port <= 0 || port > 65535) {
                return new ValidationInfo("端口必须是 1-65535 之间的数字", portField);
            }
        } catch (NumberFormatException e) {
            return new ValidationInfo("端口格式不正确", portField);
        }
        return null;
    }

    // ---------------- Getters ----------------
    public String getDbType() {
        return (String) dbTypeComboBox.getSelectedItem();
    }

    public String getHost() {
        return hostField.getText().trim();
    }

    public int getPort() {
        try {
            return Integer.parseInt(portField.getText().trim());
        } catch (NumberFormatException e) {
            return 54321;
        }
    }

    public String getDatabase() {
        return databaseField.getText().trim();
    }

    public String getUsername() {
        return usernameField.getText().trim();
    }

    public String getPassword() {
        return new String(passwordField.getPassword());
    }

    public String getAiKeyField() {
        return aiKeyField.getText().trim();
    }
    public String getJdbcUrl() {
        String type = getDbType().toLowerCase();
        String host = getHost();
        int port = getPort();
        String database = getDatabase();

        switch (type) {
            case "kingbase":
                return "jdbc:kingbase8://" + host + ":" + port + "/" + database;
            case "postgresql":
                return "jdbc:postgresql://" + host + ":" + port + "/" + database;
            case "mysql":
                return "jdbc:mysql://" + host + ":" + port + "/" + database;
            default:
                return "jdbc:kingbase8://" + host + ":" + port + "/" + database;
        }
    }

    public String getDriverClass() {
        switch (getDbType().toLowerCase()) {
            case "kingbase":
                return "com.kingbase8.Driver";
            case "postgresql":
                return "org.postgresql.Driver";
            case "mysql":
                return "com.mysql.cj.jdbc.Driver";
            default:
                return "com.kingbase8.Driver";
        }
    }

    // ---------------- UI Designer ----------------
    {
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     */
    private void $$$setupUI$$$() {
        mainPanel = new JPanel();
        mainPanel.setLayout(new GridBagLayout());
        ((GridBagLayout) mainPanel.getLayout()).columnWidths = new int[]{80, 200, 80};
        ((GridBagLayout) mainPanel.getLayout()).rowHeights = new int[]{30, 30, 30, 30, 30, 30, 30};
        ((GridBagLayout) mainPanel.getLayout()).columnWeights = new double[]{0.0, 1.0, 1.0};
        ((GridBagLayout) mainPanel.getLayout()).rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};

        JLabel dbTypeLabel = new JLabel("数据库类型:");
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        mainPanel.add(dbTypeLabel, gbc);

        dbTypeComboBox = new JComboBox<>();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mainPanel.add(dbTypeComboBox, gbc);

        JLabel hostLabel = new JLabel("主机地址:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        mainPanel.add(hostLabel, gbc);

        hostField = new JTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mainPanel.add(hostField, gbc);

        JLabel portLabel = new JLabel("端口:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        mainPanel.add(portLabel, gbc);

        portField = new JTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mainPanel.add(portField, gbc);

        JLabel databaseLabel = new JLabel("数据库名:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        mainPanel.add(databaseLabel, gbc);

        databaseField = new JTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mainPanel.add(databaseField, gbc);

        JLabel usernameLabel = new JLabel("用户名:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        mainPanel.add(usernameLabel, gbc);

        usernameField = new JTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mainPanel.add(usernameField, gbc);

        JLabel passwordLabel = new JLabel("密码:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        mainPanel.add(passwordLabel, gbc);

        passwordField = new JPasswordField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mainPanel.add(passwordField, gbc);


        JLabel aiKey = new JLabel("同义私钥Key:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        mainPanel.add(aiKey, gbc);

        aiKeyField = new JTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 6;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        mainPanel.add(aiKeyField, gbc);
    }

    public JComponent getPreferredFocusedComponent() {
        return hostField;
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }
}